﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using LevenBlog.Core.Utils;

namespace LevenBlog.Core.Data
{
    public static class ProviderManager
    {
        private static IFactory factory = null;
        private static object lockObject = new object();

        static ProviderManager()
        {
        }

        private static void LoadFactory()
        {
            //string[] names = WebConfig.ProviderName.Split(',');
            //if (names.Length != 2)
            //    throw new ReflectionTypeLoadException(null, null, "Provider name setting error.");
            //object obj = Assembly.Load(names[1].Trim()).CreateInstance(names[0].Trim());
            //if (obj == null)
            //    throw new ReflectionTypeLoadException(new Type[] { typeof(IFactory) }, null, "Load data factory error.");
            //factory = obj as IFactory;
            object obj = Activator.CreateInstance(Type.GetType(WebConfig.ProviderName));
            if (obj == null)
                throw new ReflectionTypeLoadException(new Type[] { typeof(IFactory) }, null, "Load data factory error.");
            factory = obj as IFactory;
        }

        public static IFactory Factory
        {
            get
            {
                if (factory == null)
                {
                    lock (lockObject)
                    {
                        LoadFactory();
                    }
                }
                return factory;
            }
        }
    }
}
